<?php

/**
 * @author aisyah
 * @copyright 2012
 */

//require_once "../dao/database.php";

class Product_View{
    
    var $markup;
    var $crud;
    
    function __construct(Database $class_db){
        
        $this->crud = $class_db;
        
    }
    
    
    function new_product(){
        
        date_default_timezone_set('UTC');

        $tgl = date('Y-m-d');
        $tgl_baru = strtotime('-30 day', strtotime($tgl));
		
        $date = date('Y-m-d', $tgl_baru);
        
        $this->crud->show_Data('product_detail','*',6,null,null,'productdet_id DESC');
        $hasil = $this->crud->hasil;
        $kolom = 3;
        
        $this->markup = "<div class=span6><div class='thumbnails'><div class=row>";
        
        $prd = 0;
        
        foreach($hasil as $data){                      
            
            if($prd>=3){
                $this->markup .="</div></br><div class=row>";
                $prd = 0;
            }
            $harga = number_format($data['price'],2,',','.');
            $prd++;
            $this->markup .= "<div class='span2'><div class=thumbnail align=center>
                <a class='fancybox' data-fancybox-group='gallery' href='../../images/produk/$data[image]' title='$data[product_name], Rp. $harga'  alt=''>
                <img src='../../images/produk/$data[image]' alt=''></a></br>
                <div class=caption>$data[product_name]</br>
                Rp. $harga</br></div>                
                <div class='prod_details_tab'><a class='prod_cart' href=\"product_aksi.php?modul=cart&id=$data[productdet_id]\">beli</a>
                <a class='prod_details' href='product.php?id=$data[productdet_id]'>selengkapnya</a></div></div>
                </div>";
        }
        
        $this->markup .= "</div></div></div>";
        $this->crud->hasil="";
        
        return $this->markup;
        
        
    }
    
    function most_wanted(){
        
        $this->markup = "";
        
        $this->crud->show_Data('product_detail','*',3,null,null,'count_order DESC');
        
        $hasil = $this->crud->hasil;
        
        //print_r(count($hasil));        
        
        $this->markup = "<div class=span2><div class='thumbnails'>";         
        
        foreach($hasil as $data){
            
            $harga = number_format($data['price'],2,',','.');
            
            $this->markup .= "
                <div class='span2'><div class=thumbnail align=center>
                <a class='fancybox' data-fancybox-group='gallery' href='../../images/produk/$data[image]' title='$data[product_name], Rp. $harga'>
                <img src='../../images/produk/$data[image]' border=0></a></br>
                <div class=caption><a href='product.php?id=$data[productdet_id]'>$data[product_name]</br>
                Rp. $harga</br></a></div></div> </div>               
                ";
        }
        
        $this->markup .= "</div></div>";
        
        return $this->markup;
    }
    
    function view_product($id) {
        
        $this->crud->show_Data('product_detail','*',null,null,'productdet_id='.$id);
        
        $hasil = $this->crud->hasil;
        
        //$result = array();
        
        $this->markup="";
        
        foreach($hasil as $data){
            //$result[] = $data['productdet_id'];
            
            //print_r($data);
            
            $harga = number_format($data['price'],2,',','.');
            
            $this->markup .=  "		

                
                <div class='span9'>
                <div class='row'>
                        <div class='span9'>
                            <h1>Product Title : $data[product_name]</h1>
                        </div>
                    </div>
                    <hr>

                    <div class='row'>
                        <div class='span3'>
                            <img src='../../images/produk/$data[image]' border=0 />


                            <ul class='thumbnails'>

                                <li class='span1'>
                                    <a href='#' class='thumbnail'>
                                        <img alt='' src='../../images/produk/$data[image]' />
                                    </a>
                                </li>

                                <li class='span1'>
                                    <a href='#' class='thumbnail'>
                                        <img alt='' src='../../images/produk/$data[image]' />
                                    </a>
                                </li>					

                                <li class='span1'>
                                    <a href='#' class='thumbnail'>
                                        <img alt='' src='../../images/produk/$data[image]' />
                                    </a>
                                </li>

                            </ul>

                        </div>	 

                        <div class='span6'>

                            <div class='span6'>
                                <h2>
                                    <strong>Harga: Rp $harga,-</strong> <br /><br />
                                </h2>
                            </div>	
							
							<div class='span5' halign=justify>
                                <address>
									<strong>Deskripsi:</strong> <span>$data[description]</span><br />
                                </address>
                            </div>

                            <div class='span6'>
                                <form class='form-inline'>
                                    <div class='span3 no_margin_left'>
                                        <!--<label>Qty:</label>
                                        <input type='text' class='span1' placeholder='1'>-->
                                        <div class='prod_details_tab'><a class='prod_cart' href=\"product_aksi.php?modul=cart&id=$data[productdet_id]\">beli</a></div>
                                    </div>	
                                    <!--<div class='span1'>
                                        - OR -
                                    </div>	
                                    <div class='span2'>
                                        <p><a href='#'>Add to Wish List</a></p>

                                    </div>-->	
                                </form>
                            </div>	

                            <div class='span6'>

                                <p>";
                                
                    $this->crud->hasil=null;

                    $this->crud->show_Data('review','*',null,null,'product_id='.$data['productdet_id']);
                    
                    $hasil3=$this->crud->hasil;
                    print_r($hasil3);
                    if($hasil3){
                        $jml = count($hasil3);
                    }else{
                        $jml=0;
                    }
                    
                    
                    
                    $this->markup .= "<a href='#'>$jml reviews</a>  |  <a href='#'>Write a review</a></p>
                            </div>	


                        </div>	


                    </div>
                    <hr>
                    <div class='row'>
                        <div class='span9'>
                            <div class='tabbable'>
                                <ul class='nav nav-tabs'>
                                    <li class='active'><a href='#1' data-toggle='tab'>Description</a></li>
                                    <li><a href='#2' data-toggle='tab'>Reviews</a></li>
                                    <!--<li><a href='#3' data-toggle='tab'>Related products</a></li>-->
                                </ul>
                                <div class='tab-content'>
                                    <div class='tab-pane active' id='1'>";
                                    
                                    $this->crud->hasil=null;
                                    
                                    $this->crud->show_Data('store','*',null,null,'store_id='.$data['store_id']);
                                    
                                    $hasil2 = $this->crud->hasil;
                                    
                                    
                                    foreach($hasil2 as $data2){
                                        
                                    
                                    
                                    $this->markup .= "
                                        <table class=table>
                                        <tr class=row><td colspan=2 align=center><img src='../../images/$data2[logo]'></td></tr>
                                        <tr class=row><td>Toko</td><td>$data2[store_name]</td></tr>
                                        <tr class=row><td>Keterangan</td><td>$data2[description]</td></tr>
                                        <tr class=row><td>Alamat</td><td>$data2[street]<br/>$data2[street]<br/></td></tr>                                        
                                        </table>                                        
                                        ";
                                        
                                        }
                                        
                                    $this->markup .= "</div>
                                    <div class='tab-pane' id='2'>";
                                    
                                    if($jml>0){
                                        foreach($hasil3 as $data3){
                                            $this->markup .= "<table>
                                                        <tr><b>$data3[writer]</b> at $data[date] : </tr>
                                                        <tr><p><i>$data3[review]</i></p></tr>
                                                        </table></br>";
                                        }
                                    }else{
                                        $this->markup .= "<p>There are no reviews for this product.</p>";
                                    }
                                        
                                    
                                    $this->markup .= "</div>    
                                    <!--<div class='tab-pane' id='3'>
                                        <ul class='thumbnails related_products'>

                                            <li class='span2'>
                                                <div class='thumbnail'>
                                                    <a href='product.html'><img alt='' src='http://placehold.it/220x275' /></a>
                                                    <div class='caption'>
                                                        <a href='product.html'> <h5>Produk Title</h5></a>  Price: &#36;50.00<br /><br />
                                                    </div>
                                                </div>
                                            </li>

                                            <li class='span2'>
                                                <div class='thumbnail'>
                                                    <a href='product.html'><img alt='' src='http://placehold.it/220x275' /></a>
                                                    <div class='caption'>
                                                        <a href='product.html'> <h5>Produk Title</h5></a>  Price: &#36;50.00<br /><br />
                                                    </div>
                                                </div>
                                            </li>

                                            <li class='span2'>
                                                <div class='thumbnail'>
                                                    <a href='product.html'><img alt='' src='http://placehold.it/220x275' /></a>
                                                    <div class='caption'>
                                                        <a href='product.html'> <h5>Produk Title</h5></a>  Price: &#36;50.00<br /><br />
                                                    </div>
                                                </div>
                                            </li>

                                            <li class='span2'>
                                                <div class='thumbnail'>
                                                    <a href='product.html'><img alt='' src='http://placehold.it/220x275' /></a>
                                                    <div class='caption'>
                                                        <a href='product.html'> <h5>Produk Title4</h5></a>  Price: &#36;50.00<br /><br />
                                                    </div>
                                                </div>
                                            </li>


                                        </ul>
                                    </div>-->
                                </div>
                            </div>

                        </div>
                    </div>

		</div>
  </div></div>
            ";
            
        } 
        
        
        //echo $this->markup;
        
    }
    
    function view_category($id){
        
        
        $this->crud->connect->connect();
        include "../../config/paging.php";
        
        if($id==1){
            $modul = "tkering";
        }else if($id==2){
            $modul = "tbasah";
        }  else if($id==3){
            $modul = "mkering";
        }else if($id==4){
            $modul = "mbasah";
        }else if($id==5){
            $modul = "semua";
        }      
        
        $p = new Paging;
        $batas = 5;
        $posisi = $p->cari_posisi($batas);
        
        $qcategory = "SELECT category FROM product_category WHERE productcat_id=".$id;
        
        $result = mysql_query($qcategory);
        
        if($t=mysql_fetch_array($result)){
            $category = $t['category'];
        }
        
        if($id!=5){
            $query = "SELECT 
                    product_detail.productdet_id as id,
                    product_detail.product_name as name,
                    product_detail.image as image,
                    product.product_name as type,
                    store.store_id as store_id,                    
                    store.store_name as store,
                    product_detail.price as price
                            
        
        FROM product_category INNER JOIN product ON product_category.productcat_id = product.category
                    INNER JOIN product_detail ON product.product_id = product_detail.product_id
                    INNER JOIN store ON product_detail.store_id = store.store_id
                    WHERE product_category.productcat_id=".$id." LIMIT $posisi,$batas";
        }else{
            
        
        $query = "SELECT 
                    product_detail.productdet_id as id,
                    product_detail.product_name as name,
                    product_detail.image as image,
                    product.product_name as type,
                    store.store_id as store_id,                    
                    store.store_name as store,
                    product_detail.price as price
                            
        
        FROM product_category INNER JOIN product ON product_category.productcat_id = product.category
                    INNER JOIN product_detail ON product.product_id = product_detail.product_id
                    INNER JOIN store ON product_detail.store_id = store.store_id LIMIT $posisi,$batas
                    ";
                    //print_r($query);
        }            
        $result = mysql_query($query);
        //print_r($result);
        echo "<div class='span9'>
            <h2>$category</h2>
                    </hr>
            <table class='table'>
            <thead><th>No</th><th>Nama Produk</th><th>Penampakan</th><th>Jenis</th><th>Nama Toko Penjual</th><th>Harga Rp.</th><th>Beli</th></thead>";
            $no=$posisi+1;
        while($r=mysql_fetch_array($result)){
            
            $price= number_format($r['price'],2,',','.');
            echo "<tr>
                <td>#$no</td>
                <td><a href='product.php?id=$r[id]'>$r[name]</a></td>
                <td><div class=span1><div class=thumbnail><img src='../../images/produk/$r[image]'></div></div></td>
                <td>$r[type]</td>                
                <td><a href='store.php?id=$r[store_id]'>$r[store]</a></td>
                <td>$price</td>
                <td><a class='prod_cart' href=\"product_aksi.php?modul=additem&id=$r[id]&cat=$id\">Beli</td></td>
                </tr>";
                $no++;
        }
        
        echo "<tr><td colspan=6></td></tr></table>";
        
        if($id!=5){
            $query2 = "SELECT 
                    product_detail.productdet_id as id,
                    product_detail.product_name as name,
                    product_detail.image as image,
                    product.product_name as type,
                    store.store_id as store_id,                    
                    store.store_name as store,
                    product_detail.price as price
                            
        
        FROM product_category INNER JOIN product ON product_category.productcat_id = product.category
                    INNER JOIN product_detail ON product.product_id = product_detail.product_id
                    INNER JOIN store ON product_detail.store_id = store.store_id WHERE product_category.productcat_id=".$id;
        }else{
            $query2 = "SELECT 
                    product_detail.productdet_id as id,
                    product_detail.product_name as name,
                    product_detail.image as image,
                    product.product_name as type,
                    store.store_id as store_id,                    
                    store.store_name as store,
                    product_detail.price as price
                            
        
        FROM product_category INNER JOIN product ON product_category.productcat_id = product.category
                    INNER JOIN product_detail ON product.product_id = product_detail.product_id
                    INNER JOIN store ON product_detail.store_id = store.store_id";
        }
        //print_r($query2);
        $result2 = mysql_query($query2);
        $jml_data = mysql_num_rows($result2);
        //print_r($jml_data);
        
        $jml_halaman = $p->jml_halaman($jml_data,$batas);
        //print_r($jml_halaman);
        
        $link_halaman = $p->navHalaman($modul,$_GET['halaman'],$jml_halaman);
        
        echo $link_halaman."</div></div>";
        
        $this->crud->connect->disconnect();
        
    }
    
    function tampil_toko($id_toko){
        
        $result = $crud->show_Data('store','*',null,null,'store_id='.$id_toko);
        
        if($result!=null){
            
            $toko = $result['store_name'];
            
        }else{
            $toko = "--";
            
        }
        
        return $toko;
        
        
    }
    
    
}

